<?php 
	namespace Home\Controller;

	class DirectoryController extends HomeController
	{

		public function index(){
			$shuid = I('bookid');
			// 获取用户ID
			if(!empty($_SESSION['home_user']))
			{
				$id = $_SESSION['home_user']['id'];
				$this->assign('id',$id);
				 // 查询是否收藏
			    $collect = M('collect');
			    $map['bid'] = $shuid;
			    $map['uid'] = $id;
			    $num = $collect->where($map)->count();
			    $this->assign('num',$num);
			}

			$showapi_appid = '27066'; 
		    $showapi_secret = '9e0f60470f5b4078bf41b118e7da7a7c'; 
		    $paramArr = array(
		         'showapi_appid'=> $showapi_appid,
		         'bookId'=> I('bookid'),
		         //添加其他参数
		    );

		    // var_dump(I('bookid'));

		    
		    $param = $this->createParam($paramArr,$showapi_secret);
		    $url = 'http://route.showapi.com/211-1?'.$param; 
		    $result = file_get_contents($url);
		  
		    $result = json_decode($result);

		    $book = $result->showapi_res_body->book;
		    $typeName = $book->typeName;
		    $author = $book->author;
		    $name =explode(' ',$book->name)['0'];
		    $chapterList = $book->chapterList;

		    // 小说的类型
		    $this->assign('typeName',$typeName);
		    // 小说的作者
		    $this->assign('author',$author);
		    // 小说的书名
		    $this->assign('name',$name);
		    // 小说的章节
		    $this->assign('chapterList',$chapterList);
		    // 小说的ID
		    $this->assign('bookid',$shuid);
		    // 小说的章节
		    $this->assign('zhang',count($chapterList));

		    // $Page = new \Think\PageAjax($chapterList,100);
		    // ajax返回小说的章节数据
		    // $this->ajaxreturn($chapterList);

		    $bookid = I('bookid');
		     // 查询小说详情
		    $book = M('book')->where("bookid=$bookid")->select();	
		    $this->assign('book',$book);

		    // 查询评论
			$comment = M('comment');
			// 统计查询
			$count = $comment->where("bid=$shuid")->count();
			$this->assign('count',$count);


			// 评论查询
			$data = $comment->field('jsp_comment.id,jsp_comment.content,jsp_user.name')->join('jsp_user on jsp_comment.uid=jsp_user.id')->where("bid=$shuid")->order('id desc')->select();
			// var_dump($data);

			$this->assign('list',$data);

		    $this->display();
		    // $this->display('Comment/index');
		}

	    //创建参数(包括签名的处理)
	    public function createParam ($paramArr,$showapi_secret) {
	         $paraStr = "";
	         $signStr = "";
	         ksort($paramArr);
	         foreach ($paramArr as $key => $val) {
	             if ($key != '' && $val != '') {
	                 $signStr .= $key.$val;
	                 $paraStr .= $key.'='.urlencode($val).'&';
	             }
	         }
	         $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
	         $sign = strtolower(md5($signStr));
	         $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
	        
	         return $paraStr;
	   }


	   public function add()
		{
			$data = I();
		
			// 判断用户是否登录
			if(empty($_SESSION['home_user']['name']))
			{
				$this->error('请先登录');
				exit;
			}

			// 判断内容是否为空
			if(empty($data['content']))
			{
				$this->error('评论内容不能为空');
				exit;
			}

			// 执行
			$Comment = M('comment');
			$result = $Comment->add($data);
			if ($result > 0)
			{
				$this->success('发表成功',U('Directory/index',array('bookid'=>I('bid'))),3);
	        } else{
	        	$this->error('发表失败',3);
	        }
    	}

    	public function collect(){
    		$data = I();
    		$num = $data['num'];

    		// 判断是否登录以及收藏过
    		if(empty($_SESSION['home_user']['name']))
			{
				$this->error('请先登录');
				exit;
			}

			// 判断是否收藏
			if($num != 0) {
				$this->error('你已经收藏过了');
				exit;
			}

			// 添加数据到收藏表中
			$collect = M('collect');
			$result = $collect->add($data);
			if ($result > 0)
			{
				$this->success('收藏成功',U('Directory/index',array('bookid'=>I('bid'))),3);
	        } else{
	        	$this->error('收藏失败',3);
	        }


    	}

	}